<!DOCTYPE html>
<html>
<head>
    <title>WebSocket Chat Room</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        #chat {
            border: 1px solid #ccc;
            height: 400px;
            overflow-y: scroll;
            padding: 10px;
            margin-bottom: 10px;
            background-color: #f9f9f9;
        }
        #message {
            width: 70%;
            padding: 10px;
        }
        #send {
            width: 25%;
            padding: 10px;
        }
        .message {
            margin: 5px 0;
            padding: 5px;
            border-radius: 3px;
            background-color: white;
            box-shadow: 0 1px 2px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
    <h1>WebSocket Chat Room</h1>
    <div id="chat"></div>
    <input type="text" id="message" placeholder="Type your message here..." />
    <button id="send" onclick="sendMessage()">Send</button>

    <script>
        const chatDiv = document.getElementById('chat');
        const messageInput = document.getElementById('message');
        
        // Connect to WebSocket
        const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
        const ws = new WebSocket(protocol + '//' + window.location.host + '/chat');
        
        ws.onopen = function() {
            addMessage('Connected to chat room');
        };
        
        ws.onmessage = function(event) {
            addMessage(event.data);
        };
        
        ws.onclose = function() {
            addMessage('Disconnected from chat room');
        };
        
        ws.onerror = function(error) {
            addMessage('Error: ' + error);
        };
        
        function addMessage(message) {
            const messageElement = document.createElement('div');
            messageElement.className = 'message';
            messageElement.textContent = message;
            chatDiv.appendChild(messageElement);
            chatDiv.scrollTop = chatDiv.scrollHeight;
        }
        
        function sendMessage() {
            if (messageInput.value.trim() !== '') {
                ws.send(messageInput.value);
                messageInput.value = '';
            }
        }
        
        messageInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
    </script>
</body>
</html>